using 高校员工餐饮信息管理系统.Models;

namespace 高校员工餐饮信息管理系统.Services
{
    /// <summary>
    /// 订单服务实现
    /// </summary>
    public class OrderService : IOrderService
    {
        private readonly ILogger<OrderService> _logger;
        
        public OrderService(ILogger<OrderService> logger)
        {
            _logger = logger;
        }
        
        public async Task<Order> CreateOrderAsync(int userId, int merchantId, decimal totalAmount)
        {
            _logger.LogInformation($"创建订单: 用户={userId}, 商户={merchantId}, 金额={totalAmount}");
            
            // 计算补贴金额
            var subsidyAmount = await CalculateSubsidyAsync(totalAmount, userId);
            var selfPayAmount = totalAmount - subsidyAmount;
            
            // TODO: 实际项目中应保存到数据库
            await Task.Delay(100);
            
            var order = new Order
            {
                Id = new Random().Next(1000, 9999),
                UserId = userId,
                MerchantId = merchantId,
                TotalAmount = totalAmount,
                SubsidyAmount = subsidyAmount,
                SelfPayAmount = selfPayAmount,
                OrderTime = DateTime.Now,
                Status = "已完成"
            };
            
            _logger.LogInformation($"订单创建成功: {order.Id}");
            
            return order;
        }
        
        public async Task<decimal> CalculateSubsidyAsync(decimal totalAmount, int userId)
        {
            _logger.LogInformation($"计算补贴: 金额={totalAmount}, 用户={userId}");
            
            // TODO: 实际项目中应根据用户等级和政策计算补贴
            await Task.Delay(50);
            
            // 示例：固定补贴15元，最高不超过总金额的50%
            decimal subsidyAmount = 15;
            decimal maxSubsidy = totalAmount * 0.5m;
            
            return Math.Min(subsidyAmount, maxSubsidy);
        }
        
        public async Task<List<Order>> GetUserOrdersAsync(int userId, DateTime startDate, DateTime endDate)
        {
            _logger.LogInformation($"查询用户订单: {userId}, {startDate:yyyy-MM-dd} ~ {endDate:yyyy-MM-dd}");
            
            // TODO: 实际项目中应从数据库查询
            await Task.Delay(100);
            
            var orders = new List<Order>();
            var random = new Random();
            
            for (int i = 0; i < 5; i++)
            {
                var totalAmount = random.Next(20, 50);
                var subsidyAmount = 15;
                
                orders.Add(new Order
                {
                    Id = i + 1,
                    UserId = userId,
                    MerchantId = random.Next(1, 4),
                    TotalAmount = totalAmount,
                    SubsidyAmount = subsidyAmount,
                    SelfPayAmount = totalAmount - subsidyAmount,
                    OrderTime = startDate.AddDays(random.Next(0, (endDate - startDate).Days)),
                    Status = "已完成"
                });
            }
            
            return orders;
        }
        
        public async Task<Order?> GetOrderByIdAsync(int orderId)
        {
            _logger.LogInformation($"获取订单详情: {orderId}");
            
            // TODO: 实际项目中应从数据库查询
            await Task.Delay(100);
            
            return new Order
            {
                Id = orderId,
                UserId = 1,
                MerchantId = 1,
                TotalAmount = 35,
                SubsidyAmount = 15,
                SelfPayAmount = 20,
                OrderTime = DateTime.Now.AddHours(-2),
                Status = "已完成"
            };
        }
    }
}

